package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrdersDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    OrdersService orderService;
    @Autowired
    OrdersDetailService ordersDetailService;

    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders){

        orderService.submit(orders);
        return R.success("订单提交成功");
    }

    @GetMapping("userPage")
    public R<Page<OrdersDto>> page(Integer page, Integer pageSize){
        Page<Orders> pageInfo1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class);
           orderService.page(pageInfo1,queryWrapper);

        Page<OrdersDto> pageInfo2 = new Page<>(page, pageSize);
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        List<OrdersDto> collect = pageInfo1.getRecords().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            LambdaQueryWrapper<OrderDetail> wrapper = Wrappers.lambdaQuery(OrderDetail.class)
                    .eq(OrderDetail::getOrderId, orders.getId())
                    .orderByDesc(OrderDetail ::getAmount);
            List<OrderDetail> orderDetails = ordersDetailService.list(wrapper);
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());

     pageInfo2.setRecords(collect);
        return R.success(pageInfo2);
    }
// 后台订单界面展示
@GetMapping("page")
    public R<Page<Orders>> page(Integer page, Integer pageSize, Long number, LocalDateTime beginTime,LocalDateTime endTime){
    Page<Orders> pageInfo1 = new Page<>(page, pageSize);
    LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class)
            .eq(number != null, Orders::getId, number);
   orderService.page(pageInfo1, queryWrapper);

    List<Orders> ordersList = orderService.list(queryWrapper);
    for (Orders orders : ordersList) {
        orders.setUserName(orders.getConsignee());
    }
    pageInfo1.setRecords(ordersList);

    return R.success(pageInfo1);
}
/*
后台判断是否将订单派送取出,status=3
*/

@PutMapping
    public R<String> status(@RequestBody Orders orders){
    LambdaUpdateWrapper<Orders> updateWrapper = Wrappers.lambdaUpdate(Orders.class)
            .set(Orders::getStatus, orders.getStatus())
            .eq(Orders::getId, orders.getId());

    orderService.update(updateWrapper);

    return R.success("状态更新成功");
}
/*
* 再来一单
* */
@PostMapping("again")
    public R<String> again(@RequestBody Orders orders){
    LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class)
            .eq(Orders::getId, orders.getId());
    Orders orders1 = orderService.getOne(queryWrapper);
    LambdaQueryWrapper<OrderDetail> queryWrapper1 = Wrappers.lambdaQuery(OrderDetail.class)
            .eq(OrderDetail::getOrderId, orders.getId());
    List<OrderDetail> orderDetailList = ordersDetailService.list(queryWrapper1);
    Long id = IdWorker.getId();
    orders1.setId(id);
    orders1.setOrderTime(LocalDateTime.now());
    orders1.setCheckoutTime(LocalDateTime.now());
    orderDetailList.forEach(item->item.setOrderId(id));
    orderDetailList.forEach(item->item.setId(null));
    orderService.save(orders1);
    ordersDetailService.saveBatch(orderDetailList);
    return R.success("再来一单成功");
}
}
